

from concurrent import futures

import grpc

from demo import server_stream_pb2_grpc, server_stream_pb2

class SearchServicer(server_stream_pb2_grpc.SearchServicer):
    def SearchStream(self, request, context):
        keyword = request.keyword
        # 模拟 3 条结果，间隔 1 秒推送
        for i in range(1, 40):
            #time.sleep(1)
            yield server_stream_pb2.Result(
                title=f'{keyword} result #{i}',
                url=f'https://example.com/{keyword}/{i}'
            )

if __name__ == '__main__':
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    server_stream_pb2_grpc.add_SearchServicer_to_server(
        SearchServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    print('Server listening on :50051')
    server.wait_for_termination()